package com.easthome.process.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easthome.common.handler.EasthomeException;
import com.easthome.common.result.Result;
import com.easthome.model.process.ProcessTemplate;
import com.easthome.model.process.ProcessType;
import com.easthome.process.service.ProcessTemplateService;
import com.easthome.process.service.ProcessTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.io.Resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/15 14:36
 */
@Api(value = "审批模板管理", tags = "审批模板管理")
@RestController
@RequestMapping("/admin/process/processTemplate")
public class ProcessTemplateController {
   @Autowired
    private ProcessTemplateService processTemplateService;
   @Autowired
   private ProcessTypeService processTypeService;
   @ApiOperation("分页查找")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(@PathVariable("page") Long page,
                              @PathVariable("limit") Long limit){
       Page<ProcessTemplate> processTemplatePage = new Page<>(page, limit);
       processTemplateService.selectPage(processTemplatePage);
       return Result.ok(processTemplatePage);
   }
    @PreAuthorize("hasAuthority('bnt.processTemplate.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ProcessTemplate processTemplate = processTemplateService.getById(id);
        return Result.ok(processTemplate);
    }

    @PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result save(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.save(processTemplate);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.processTemplate.templateSet')")
    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result updateById(@RequestBody ProcessTemplate processTemplate) {
        processTemplateService.updateById(processTemplate);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.processTemplate.remove')")
    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        processTemplateService.removeById(id);
        return Result.ok();
    }
    @ApiOperation("查询所有审批类型")
    @GetMapping("/findAll")
    public Result findAll(){
        List<ProcessType> list = processTypeService.list();
        return Result.ok(list);
    }
    @ApiOperation("接收文件")
    @PostMapping("uploadProcessDefinition")
    public Result uploadProcessDefinition(@RequestParam("file") MultipartFile multipartFile)throws FileNotFoundException{
        String absolutePath = new File(ResourceUtils.getURL("classpath:").getPath()).getAbsolutePath();
        File file = new File(absolutePath + "/process/");
        //判断目录是否存在
        if(!file.exists()){
            file.mkdirs();//创建目录
        }
        String fileName = multipartFile.getOriginalFilename();
        File newFile = new File(absolutePath + "/process/" + fileName);
        try {
            multipartFile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("文件上传失败");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("processDefinitionPath","process/"+fileName);
        map.put("processDefinitionKey",fileName.substring(0,fileName.lastIndexOf(".")));
        return Result.ok(map);
    }
    @PreAuthorize("hasAuthority('bnt.processTemplate.publish')")
    @ApiOperation(value = "发布")
    @GetMapping("/publish/{id}")
    public Result publish(@PathVariable Long id) {
        processTemplateService.publish(id);
        return Result.ok();
    }


}
